﻿namespace EFS.RC.CommunicatorContext.Operators
{
	public partial class CustomerSurvey : EFS.RC.CommunicatorContext.Common.Pages.Controls.Control
	{
		/// Class RequestCustomerSurveyArgs
		public class RequestCustomerSurveyArgs : System.EventArgs
		{
			public RequestCustomerSurveyArgs()
			{
			}
		}

		public class Item { public System.Int32? ID { get; set; } public System.String Name { get; set; } }

		#region EventHandlers
		/// Event used when a message has been received
		public event System.EventHandler<EFS.RC.CommunicatorContext.Operators.CustomerSurvey.RequestCustomerSurveyArgs> RequestCustomerSurvey;
		#endregion

		#region Delegates
		/// SelectionChanged
		public delegate void SelectionChangedDelegate(System.Int32? newSelectionID);
		public SelectionChangedDelegate SelectionChanged;
		#endregion

		public CustomerSurvey()
		{
			InitializeComponent();

			// ignore in design tool
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				// set assync handlers
				UtilitiesServiceSurvey.Survey_GetAllCompleted += new System.EventHandler<EFS.RC.Survey.Controls.Utilities.Survey_GetAllCompletedEventArgs>(UtilitiesServiceSurvey_Survey_GetAllCompleted);
			}
		}

		#region Properties

		/// Property SelectedID: Get
		public System.Int32? SelectedID
		{
			get
			{
				// test
				if (null != comboBoxCustomerSurvey.SelectedValue)
				{
					// ID
					return (System.Int32?)comboBoxCustomerSurvey.SelectedValue;
				}

				// none
				return null;
			}
		}

		/// Property ReadOnly: Get, Set
		public System.Boolean ReadOnly
		{
			get
			{
				return !comboBoxCustomerSurvey.IsEnabled;
			}
			set
			{
				comboBoxCustomerSurvey.IsEnabled = !value;
				buttonRequestCustomerSurvey.IsEnabled = !value;
			}
		}

		#endregion

		#region Usage

		/// <summary>
		/// Load 
		/// </summary>
		/// <param name="lineId"></param>
		private void LoadData()
		{
			// load survey object
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(comboBoxCustomerSurvey, "Loading Survey");

			// get surveys
			UtilitiesServiceSurvey.Survey_GetAllAsync(
				0,
				(System.Int32)EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType.Conversation,
				(System.Int32)EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType.Customer);
		}

		#endregion

		#region Events
		/// Loaded
		private void Control_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			// ignore in designer
			if (System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				return;
			}

			// load data
			LoadData();
		}

		/// Selection changed
		private void comboBoxCustomerSurvey_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
		{
			// notify all registered
			if (null != this.SelectionChanged)
			{
				this.SelectionChanged(SelectedID);
			}
		}

		/// buttonRequestCustomerSurvey_Click
		private void buttonRequestCustomerSurvey_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			if (null != RequestCustomerSurvey)
			{
				// notify
				this.RequestCustomerSurvey(buttonRequestCustomerSurvey, new RequestCustomerSurveyArgs());
			}
		}
		#endregion

		#region Callbacks
		/// UtilitiesServiceSurvey_Survey_GetAllCompleted
		private void UtilitiesServiceSurvey_Survey_GetAllCompleted(object sender, EFS.RC.Survey.Controls.Utilities.Survey_GetAllCompletedEventArgs e)
		{
			// close wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(comboBoxCustomerSurvey);

			//check if request was successful
			if (null != e.Error)
			{
				ShowErrorMessage("Failed to get survey.", e.Error);
			}

			try
			{
				// get array
				EFS.RC.CC.CH.Entities.Surveys.SurveyInfo[] array =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo[]>(e.Result.ToString());

				// create array
				System.Collections.Generic.List<Item> itemList = new System.Collections.Generic.List<Item>();
				itemList.Add(new Item { ID = null, Name = "Select to start" });

				// cicle all
				foreach (EFS.RC.CC.CH.Entities.Surveys.SurveyInfo surveyInfo in array)
				{
					itemList.Add(new Item { ID = surveyInfo.ID, Name = surveyInfo.Name });
				}

				comboBoxCustomerSurvey.ItemsSource = itemList.ToArray();
				comboBoxCustomerSurvey.SelectedIndex = 0;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to get customer surveys. " + ex.Message);
			}
		}
		#endregion
	}

}